package org.example.core.utils;


/**
 * @author 小杰_cc
 * @version 1.0
 */

/**
 * 统一结果返回
 */
public class R<T> {
    private static final long serialVersionUID = 1L;

    /**
     * 请求成功
     */
    public static final int SUCCESS = 0;
    public static final String SUCCESS_MESSAGE = "success";

    /**
     * 请求失败
     */
    private static final int ERROR = 1;
    private static final String ERROR_MESSAGE = "error";

    private int code;

    private String msg;

    private T data;

    private static <T> R<T> result(int code, String msg, T data){
        R<T> result = new R<>();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    public static <T> R<T> ok(){
        return result(SUCCESS, SUCCESS_MESSAGE, null);
    }

    public static <T> R<T> ok(T data){
        return result(SUCCESS, SUCCESS_MESSAGE, data);
    }

    public static <T> R<T> error() {
        return result(ERROR, ERROR_MESSAGE, null);
    }

    public static <T> R<T> error(String msg)
    {
        return result(ERROR, msg, null);
    }

    public static <T> R<T> error(int code, String msg) {
        return result(code, msg,null );
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

